package com.leyou.system.controller;

import com.leyou.common.domain.PageResult;
import com.leyou.system.dto.PermissionDTO;
import com.leyou.system.dto.RoleDTO;
import com.leyou.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    //@Secured("ROLE_USER")
    @GetMapping("/page")
    public ResponseEntity<PageResult<RoleDTO>> rolePageQuery(@RequestParam( value = "page",defaultValue = "1")  Integer page ,
                                        @RequestParam(value ="rows" ,defaultValue = "5") Integer rows,
                                        @RequestParam(value = "key" ,required = false) String key,
                                         @RequestParam(value = "sortBy",required = false) String sortBy,
                                        @RequestParam(value = "desc",defaultValue = "false") boolean desc){
        PageResult<RoleDTO> pageResult= roleService.rolePageQuery(page,rows,key,sortBy,desc);
        return ResponseEntity.ok(pageResult);
    }

    //@Secured("ROLE_USER")
    @GetMapping("/{id}")
    private ResponseEntity<RoleDTO> findRoleById(@PathVariable("id") String id){

        RoleDTO roleDTO=  roleService.findRoleById(id);

        return  ResponseEntity.ok(roleDTO);
    }

    //@Secured("ROLE_USER")
    @PostMapping
    public ResponseEntity<Void> saveRole(
            @RequestParam("roleName") String roleName,
            @RequestParam("roleDesc") String roleDesc,
            @RequestParam("pids")List<Integer> pids){

            roleService.saveRole(roleName,roleDesc,pids);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //修改角色
    //@Secured("ROLE_USER")
    @PutMapping
    public ResponseEntity<Void>updateRole(@RequestParam("id") Integer id,
                                          @RequestParam("roleName") String roleName,
                                          @RequestParam("roleDesc") String roleDesc,
                                          @RequestParam("pids")List<Integer> pids){
        roleService.updateRole(id,roleName,roleDesc,pids);
        return ResponseEntity.status(HttpStatus.OK).build();
    }


    @GetMapping
    //@Secured("ROLE_USER")
    public ResponseEntity<List<RoleDTO>> findAllRole(){
        List<RoleDTO> list = roleService.findAllRole();
        return ResponseEntity.ok(list);
    }


    //@Secured("ROLE_USER")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRoleById(@PathVariable("id") Integer id){
        roleService.deleteRoleById(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @GetMapping("/permission")
    //@Secured("ROLE_USER")
    public ResponseEntity<List<PermissionDTO>> queryPermissionByRoleId(@RequestParam("rid")Integer rid) {
        List<PermissionDTO> permissionDTOList = roleService.queryPermissionByRoleId(rid);
        return ResponseEntity.ok(permissionDTOList);
    }

}
